package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.annotation.DataScope;
import com.xjy.common.exception.UtilException;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.utils.SecurityUtils;
import com.xjy.system.domain.FundLedger;
import com.xjy.system.domain.SysContractResult;
import com.xjy.system.domain.SysUserRole;
import com.xjy.system.domain.bo.SysContractResultQueryBo;
import com.xjy.system.domain.vo.FundLedgerVo;
import com.xjy.system.domain.vo.SysContractResultVo;
import com.xjy.system.service.ISysBudgetService;
import com.xjy.system.service.ISysProjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.bo.SysBudgetMngAddBo;
import com.xjy.system.domain.bo.SysBudgetMngQueryBo;
import com.xjy.system.domain.bo.SysBudgetMngEditBo;
import com.xjy.system.domain.SysBudgetMng;
import com.xjy.system.mapper.SysBudgetMngMapper;
import com.xjy.system.domain.vo.SysBudgetMngVo;
import com.xjy.system.service.ISysBudgetMngService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 预算管理Service业务层处理
 *
 * @author zc
 * @date 2022-02-12
 */
@Service
public class SysBudgetMngServiceImpl extends ServicePlusImpl<SysBudgetMngMapper, SysBudgetMng> implements ISysBudgetMngService {
	@Autowired
	private ISysBudgetService iSysBudgetService;
	@Autowired
	private ISysProjService iSysProjService;

	@Override
	public SysBudgetMngVo queryById(Long id) {
		SysBudgetMngVo rsp = null;

		SysBudgetMngQueryBo sysBudgetMngQueryBo = new SysBudgetMngQueryBo();
		sysBudgetMngQueryBo.setId(id);
		List<SysBudgetMngVo> list = baseMapper.queryPageList(sysBudgetMngQueryBo,null);
		if (null != list && list.size() > 0) {
			rsp = list.get(0);
		}
		return rsp;
	}

	@DataScope(budget = "bm")
	@Override
	public TableDataInfo<SysBudgetMngVo> queryPageList(SysBudgetMngQueryBo bo) {
		Long id = SecurityUtils.getLoginUser().getUser().getUserId();

		if (SecurityUtils.isAdmin(id)) {
			List<SysBudgetMngVo> list = baseMapper.queryPageList(bo,null);
			return PageUtils.buildDataInfo(list);
		} else {
			Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
			List<SysBudgetMngVo> list = baseMapper.queryPageList(bo,SecurityUtils.getLoginUser().getUser().getDeptId());
			return PageUtils.buildDataInfo(list);
		}
	}

	@Override
	public List<SysBudgetMngVo> queryList(SysBudgetMngQueryBo bo) {
		return listVo(buildQueryWrapper(bo), SysBudgetMngVo.class);
	}

	private LambdaQueryWrapper<SysBudgetMng> buildQueryWrapper(SysBudgetMngQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<SysBudgetMng> lqw = Wrappers.lambdaQuery();
		lqw.like(StrUtil.isNotBlank(bo.getName()), SysBudgetMng::getName, bo.getName());
		lqw.eq(bo.getAmount() != null, SysBudgetMng::getAmount, bo.getAmount());
		lqw.eq(StrUtil.isNotBlank(bo.getInterpretation()), SysBudgetMng::getInterpretation, bo.getInterpretation());
		lqw.eq(bo.getProjId() != null, SysBudgetMng::getProjId, bo.getProjId());
		lqw.eq(StrUtil.isNotBlank(bo.getAttribute()), SysBudgetMng::getAttribute, bo.getAttribute());
		lqw.eq(StrUtil.isNotBlank(bo.getProjName()), SysBudgetMng::getProjName, bo.getProjName());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(SysBudgetMngAddBo bo) {
		SysBudgetMng add = BeanUtil.toBean(bo, SysBudgetMng.class);
		if (null == add.getAmount() || add.getAmount().compareTo(new BigDecimal(0)) < 1) {
			throw new UtilException("预算金额必须大于0！");
		}
		add.setName(iSysBudgetService.getByIdInfo(bo.getBudgetId()).getSubjectName());
		add.setProjName(iSysProjService.queryById(bo.getProjId()).getName());
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(SysBudgetMngEditBo bo) {
		SysBudgetMng update = BeanUtil.toBean(bo, SysBudgetMng.class);
		if (null == update.getAmount() || update.getAmount().compareTo(new BigDecimal(0)) < 1) {
			throw new UtilException("预算金额必须大于0！");
		}
		update.setName(iSysBudgetService.getByIdInfo(bo.getBudgetId()).getSubjectName());
		update.setProjName(iSysProjService.queryById(bo.getProjId()).getName());
		validEntityBeforeSave(update);
		return updateById(update);
	}

	@Override
	public Boolean auditByEditBo(SysBudgetMngEditBo bo) {
		SysBudgetMngVo sysBudgetMngVo = this.queryById(bo.getId());
		if (sysBudgetMngVo.getAuditStatus()) {
			throw new UtilException("该数据无需重新审批！");
		}

		SysBudgetMng update = BeanUtil.toBean(bo, SysBudgetMng.class);
		update.setAuditStatus(true);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(SysBudgetMng entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
